﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Threading;
using ClipShow2.MediaFinder;
using ClipShow2.MediaTypes;
using ClipShow2.Storage;

namespace ClipShow2.LibraryManager
{
    public class NotifierEventArgs : EventArgs
    {
        private ClipShowContent _eventLibrary;

        public NotifierEventArgs(ClipShowContent eventLibrary)
        {
            _eventLibrary = eventLibrary;
        }

        public ClipShowContent NotifierLibrary
        {
            get { return(_eventLibrary);}
        }
    }
    public delegate void NotifierEventHandler(object sender, NotifierEventArgs e);

    /// <summary>
    /// Exception notification class
    /// </summary>
    public class NotifierExceptionArgs : EventArgs
    {
        private string _errText;

        public NotifierExceptionArgs(string errText)
        {
            _errText = errText;
        }

        public string NotifierExceptionText
        {
            get
            {
                return (_errText);
            }
        }
    }

    public delegate void NotifierExceptionHandler(object sender, NotifierExceptionArgs e);

    public class Notifier
    {
        public Notifier()
        {
        }

        public event NotifierEventHandler NotifierAlert;
        public event NotifierExceptionHandler NotifierException;
        protected virtual void OnNotifierAlert(NotifierEventArgs e)
        {
            if (NotifierAlert != null)
            {
                // Invokes the delegates. 
                NotifierAlert(this, e);
            }
        }
        protected virtual void OnNotifierExceptionAlert(NotifierExceptionArgs e)
        {
            if (NotifierException != null)
            {
                // Invokes the delegates. 
                NotifierException(this, e);
            }
        }
        public void Start()
        {
            Thread _localMedia;
            _localMedia = new Thread(new ThreadStart(CollectMedia));
            _localMedia.Name = "mce_ClipShow_MediaCollection";
            _localMedia.IsBackground = false;
            _localMedia.Priority = ThreadPriority.Normal;
            _localMedia.Start();
        }


        /// <summary>
        /// Search for media
        /// </summary>
        private void CollectMedia()
        {
            try
            {
                StorageEngine.Initialize();
                ClipShowContent csc = SearchEngine.QueryWindowsSearchLocal("");
                
                // Fire event notifying delegates that local media has been found
                NotifierEventArgs local = new NotifierEventArgs(csc);
                OnNotifierAlert(local);

                // Check for WHS configuration on local machine
                if (StorageEngine.GetSettings().WHSAvailable)
                {
                    if (StorageEngine.GetSettings().IncludeWindowsHomeServer)
                    {
                        try
                        {
                            ClipShowContent cscWHS = SearchEngine.QueryWindowsSearchWHS("");
                            if (cscWHS.MediaItems.Count > 0)
                            {
                                NotifierEventArgs whs = new NotifierEventArgs(cscWHS);
                                OnNotifierAlert(whs);
                            }
                        }
                        catch
                        {
                            // No notification
                        }
                    }

                }

                // Check HomeGroup
                if (StorageEngine.GetSettings().IncludeHomegroup)
                {
                    try
                    {
                        ClipShowContent cscHG = SearchEngine.QueryWindowsSearchHG("");
                        if (cscHG.MediaItems.Count > 0)
                        {
                            NotifierEventArgs hg = new NotifierEventArgs(cscHG);
                            OnNotifierAlert(hg);
                        }

                    }
                    catch
                    {
                        // No notification
                    }
                }

            }
            catch (SystemException se)
            {
                // Fire exception event notifying delegates that something has gone wrong
                NotifierExceptionArgs err = new NotifierExceptionArgs(se.Message);
                OnNotifierExceptionAlert(err);
            }

            // Finish
            NotifierExceptionArgs complete = new NotifierExceptionArgs("Finish");
            OnNotifierExceptionAlert(complete);
        }
    }
}
